package com.goodving.guli.bt.utils;

import android.content.Context;
import android.os.AsyncTask;
import android.util.SparseArray;

import com.goodving.guli.R;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.lua.DaoManager;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.weight.LuaUtil;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.bt.weight.WeightGoalManager;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.utility.gulihardware.Constant;
import com.goodving.guli.view.BitmapDescriptorFactory;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.dao.query.WhereCondition;
import de.greenrobot.gulidao.LuaList;
import de.greenrobot.gulidao.LuaListDao;


/**
 * Created by john on 2015/12/1.
 */
public class WeightUtil { // aA
    public static final int f11299a = 0;
    public static final int f11300b = 1;
    public static final int f11301c = -1;
    public static final int f11302d = 2;
    public static final int f11303e = -2;
    public static final String TAG = "WeightUtil";
    public static final float f11305g = 18.5f;
    public static final float f11306h = 24.0f;
    public static final float f11307i = 28.0f;
    public static final int f11308j = 3;
    public static boolean f11309k;
    public static final float[] f11310l;
    public static final float[] f11311m;
    public static final float[] f11312n;
    public static final float[] f11313o;
    private static HashMap<WeightLevel, String> f11314p;
    private static HashMap<WeightLevel, String> f11315q;
    private static HashMap<WeightLevel, String> f11316r;
    private static HashMap<WeightLevel, Integer> f11317s;

    static {
        f11314p = null;
        f11315q = null;
        f11316r = null;
        f11317s = null;
        f11310l = new float[]{17.4f, 18.1f, 18.9f, 19.6f, 20.3f, 21.0f, 21.9f, 22.6f, 23.1f, 23.5f, 23.8f, f11306h};
        f11311m = new float[]{19.2f, 20.3f, 21.4f, 22.5f, 23.6f, 24.7f, 25.7f, 26.4f, 26.9f, 27.4f, 27.8f, f11307i};
        f11312n = new float[]{17.2f, 18.1f, 19.0f, 20.0f, 21.1f, 21.9f, 22.6f, 23.0f, 23.4f, 23.7f, 23.8f, f11306h};
        f11313o = new float[]{18.9f, 19.9f, 21.0f, 22.1f, 23.3f, 24.5f, 25.6f, 26.3f, 26.9f, 27.4f, 27.7f, f11307i};
    }

    public static float m15687a(float f) {
        return m15716c(f, f11300b);
    }

    public static float m15688a(float f, float f2, int i) {
        return m15717c((f * f2) * f2, f11299a, i);
    }

    public static float m15689a(float f, int i) {
        return m15717c(f, i, f11299a);
    }

    public static float m15690a(float f, int i, float f2) {
        float f3 = (f < 1.0f || (i != f11301c && i <= 6)) ? -1.0f : f2 / (f * f);
        return m15687a(f3);
    }

    public static float m15691a(int i) {
        return i == 0 ? 150.0f : i == 16 ? BitmapDescriptorFactory.HUE_MAGENTA : i == f11300b ? 331.1f : 150.0f;
    }

    public static float m15692a(int i, int i2) {
        return i2 == f11300b ? f11310l[i - 7] : i2 == 0 ? f11312n[i - 7] : -1.0f;
    }

    public static float m15693a(int i, int i2, float f) {
        return m15690a(((float) i) / 100.0f, i2, f);
    }

    public static float m15694a(UserInfo userInfo, float f) {
        return m15693a(userInfo.height, Birthday.fromStr(userInfo.birthday).getAge(), f);
    }

    public static int m15695a(float f, int i, int i2) {
        if (f11317s == null) {
            f11317s = new HashMap();
            f11317s.put(WeightLevel.Light, Integer.valueOf(-15685185));
            f11317s.put(WeightLevel.Normal, Integer.valueOf(-8476672));
            f11317s.put(WeightLevel.Weight, Integer.valueOf(-24525));
            f11317s.put(WeightLevel.Fat, Integer.valueOf(-42171));
        }
        return ((Integer) f11317s.get(m15708b(f, i, i2))).intValue();
    }

    public static String m15696a(long j, int i) {
        return "weight_" + j + "_" + i;
    }

    public static String m15697a(Context context, float f, int i, int i2) {
        String[] stringArray = context.getResources().getStringArray(R.array.weight_body_figures);
        if (i > 6 && i < 18) {
            stringArray = context.getResources().getStringArray(R.array.weight_body_figures_for_child);
        }
        HashMap hashMap = new HashMap();
        hashMap.put(WeightLevel.Light, stringArray[f11299a]);
        hashMap.put(WeightLevel.Normal, stringArray[f11300b]);
        hashMap.put(WeightLevel.Weight, stringArray[f11302d]);
        hashMap.put(WeightLevel.Fat, stringArray[f11308j]);
        String str = (String) hashMap.get(m15708b(f, i, i2));
        return str == null ? "--" : str;
    }

    public static String m15698a(Context context, int i) {
        SparseArray sparseArray = new SparseArray();
        String[] stringArray = context.getResources().getStringArray(R.array.weight_unit);
        sparseArray.put(f11299a, stringArray[f11299a]);
        sparseArray.put(16, stringArray[f11300b]);
        sparseArray.put(f11300b, stringArray[f11302d]);
        String str = (String) sparseArray.get(i);
        return str == null ? (String) sparseArray.get(f11299a) : str;
    }

    public static String m15699a(Context context, int i, int i2, float f) {
        String[] stringArray = context.getResources().getStringArray(R.array.weight_report_suggestions_girl);

        if (f11314p == null) {
            f11314p = new HashMap();
            f11314p.put(WeightLevel.Light, stringArray[f11299a]);
            f11314p.put(WeightLevel.Normal, stringArray[f11300b]);
            f11314p.put(WeightLevel.Weight, stringArray[f11302d]);
            f11314p.put(WeightLevel.Fat, stringArray[f11308j]);
        }
        if (f11315q == null) {
            f11315q = new HashMap();
            stringArray = context.getResources().getStringArray(R.array.weight_report_suggestions_boy);
            f11315q.put(WeightLevel.Light, stringArray[f11299a]);
            f11315q.put(WeightLevel.Normal, stringArray[f11300b]);
            f11315q.put(WeightLevel.Weight, stringArray[f11302d]);
            f11315q.put(WeightLevel.Fat, stringArray[f11308j]);
        }
        return i2 == f11300b ? (String) f11315q.get(m15708b(f, i, i2)) : (String) f11314p.get(m15708b(f, i, i2));
    }

    public static String m15700a(Context context, UserInfo userInfo, ScaleWeightInfo c3925e) {
        float c;
        Object[] objArr;
        if (userInfo.targetWeight > 0.0f) {
            c = m15716c(((float) ((int) m15706b(userInfo.targetWeight, c3925e.m25497h()))) - c3925e.m25499j(), f11300b);
            if (c > 0.0f) {
                objArr = new Object[f11300b];
                objArr[f11299a] = Math.abs(c) + m15698a(context, c3925e.m25497h());
                return context.getString(R.string.lighter_than_goal, objArr);
            } else if (c < 0.0f) {
                objArr = new Object[f11300b];
                objArr[f11299a] = Math.abs(c) + m15698a(context, c3925e.m25497h());
                return context.getString(R.string.heavier_than_goal, objArr);
            }
        } else if (userInfo.uid != 0) {
            WeightInfo a = WeightInfoManager.getInstance().m15744a(userInfo.uid, c3925e.m25494e());
            if (a == null) {
                return context.getString(R.string.baby_no_weight);
            }
            c = m15716c(c3925e.m25499j() - m15706b(a.weight, c3925e.m25497h()), f11300b);
            if (c > 0.0f) {
                objArr = new Object[f11300b];
                objArr[f11299a] = c + m15698a(context, c3925e.m25497h());
                return context.getString(R.string.add_than_last_time, objArr);
            } else if (c >= 0.0f) {
                return context.getString(R.string.equal_last_time);
            } else {
                objArr = new Object[f11300b];
                objArr[f11299a] = Math.abs(c) + m15698a(context, c3925e.m25497h());
                return context.getString(R.string.less_than_last_time, objArr);
            }
        }
        return C3657a.f18328f;
    }

    public static List<UserInfo> m15701a(ScaleWeightInfo c3925e) {
        List<UserInfo> arrayList = new ArrayList();
        List<UserInfo> d = UserInfoManager.getInstance().m15598d();
        WeightInfoManager a = WeightInfoManager.getInstance();
        for (UserInfo userInfo : d) {
            WeightInfo e = a.m15777e(userInfo.uid);
            if (e != null) {
                userInfo.weight = e.weight;
                GuliLog.tracePrintInfo(TAG, "lastest user:" + userInfo.uid + ",weight:" + e.weight);
            }
        }
        if (d == null || d.size() < f11300b) {
            GuliLog.tracePrintInfo(TAG, "no user info for weight!!!");
        } else {
            for (UserInfo userInfo2 : d) {
                GuliLog.tracePrintInfo(TAG, "info:" + userInfo2);
                // important, ~3kg to check the person weight
                if (Math.abs(userInfo2.weight - m15689a(c3925e.m25499j(), c3925e.m25497h())) < 3.0f) {
                    arrayList.add(userInfo2);
                }
            }
        }
        GuliLog.m12370d(TAG, "matched user info size:" + arrayList.size());
        return arrayList;
    }

    public static void deleteWeightInfoMarkFully(WeightInfo weightInfo) { // m15703a
        GuliLog.tracePrintInfo(TAG, "deleteWeightInfoMarkFully:" + weightInfo);
        deleteDynamicItem(m15696a(weightInfo.timestamp, weightInfo.uid));
        WeightInfoManager.getInstance().m15770c(weightInfo);
    }

    public static void deleteDynamicItem(String str) { // m15704a
        GuliLog.tracePrintInfo(TAG, "deleteDynamicItem type is " + str);
        LuaListDao b = DaoManager.getInstance().getLuaListDao();
        List list = b.queryBuilder().where(LuaListDao.Properties.Type.eq(str), new WhereCondition[f11299a]).list();
        if (list != null && list.size() > 0 && list.get(f11299a) != null) {
            GuliLog.tracePrintInfo(TAG, "delete lua list:" + list.get(f11299a));
            b.delete((LuaList)list.get(f11299a));
        }
    }

    public static boolean syncWithServer(Context context) { // m15705a
        boolean a = UserInfoManager.getInstance().syncWithServer(context);
        GuliLog.m12370d(TAG, "Sync UserInfos To Server : " + a);
        if (!a) {
            return false;
        }
        Boolean b = WeightInfoManager.getInstance().syncWithServer(context);
        GuliLog.m12370d(TAG, "Sync WeightInfos To Server : " + b);
        return b;
    }

    public static float m15706b(float f, int i) {
        return m15717c(f, f11299a, i);
    }

    public static float m15707b(int i, int i2) {
        return i2 == f11300b ? f11311m[i - 7] : i2 == 0 ? f11313o[i - 7] : -1.0f;
    }

    public static WeightLevel m15708b(float f, int i, int i2) {
        GuliLog.tracePrintInfo(TAG, "bmi " + f + " age " + i + " gender  " + i2);
        return (i < 0 || i > 6) ? (i <= 6 || i > 18) ? i > 18 ? f <= 0.0f ? WeightLevel.Unknown : f < f11305g ? WeightLevel.Light : f < f11306h ? WeightLevel.Normal : f < f11307i ? WeightLevel.Weight : WeightLevel.Fat : WeightLevel.Normal : i2 == f11300b ? f < f11310l[i + -7] ? WeightLevel.Normal : (f < f11310l[i + -7] || f >= f11311m[i - 7]) ? WeightLevel.Fat : WeightLevel.Weight : i2 == 0 ? f < f11312n[i + -7] ? WeightLevel.Normal : (f < f11312n[i + -7] || f >= f11313o[i - 7]) ? WeightLevel.Fat : WeightLevel.Weight : WeightLevel.Unknown : WeightLevel.Unknown;
    }

    public static String m15709b(float f) {
        return f > 0.0f ? String.valueOf(f) : "--";
    }

    public static String m15710b(int i, int i2, float f) {
        return m15709b(m15693a(i, i2, f));
    }

    public static String m15711b(Context context, float f, int i, int i2) {
        if (f11316r == null) {
            f11316r = new HashMap();
            String[] stringArray = context.getResources().getStringArray(R.array.weight_dynamic_list_bmi_tips);
            f11316r.put(WeightLevel.Light, stringArray[f11299a]);
            f11316r.put(WeightLevel.Normal, stringArray[f11300b]);
            f11316r.put(WeightLevel.Weight, stringArray[f11302d]);
            f11316r.put(WeightLevel.Fat, stringArray[f11308j]);
        }
        return (String) f11316r.get(m15708b(f, i, i2));
    }

    static class aC implements aE {
        final /* synthetic */ aB f11319a;

        aC(aB aBVar) {
            this.f11319a = aBVar;
        }

        public void onSuccess () { // m15722a
//            QQHealth qQHealth = null;
//            if (C0815b.m8876h().f4193c.f4248d.booleanValue()) {
//                qQHealth = QQHealth.getInstance();
//                if (qQHealth != null) {
//                    aA.m15714b(qQHealth);
//                }
//            }
            UserInfoManager.getInstance().syncWithServer(this.f11319a.f11318a);
            WeightGoalManager.getInstance().syncLocalToServer(this.f11319a.f11318a);
            WeightInfoManager.getInstance().syncFromServer(this.f11319a.f11318a);
//            if (qQHealth != null) {
//                qQHealth.syncDataWeight();
//            }
        }

        public void onFailure () { // m15723b
        }
    }

    static final class aB extends AsyncTask<Void, Void, Void> {
        final /* synthetic */ Context f11318a;

        aB(Context context) {
            this.f11318a = context;
        }

        protected Void m15720a(Void... voidArr) {
            WeightUtil.f11309k = true;
            UserInfoManager.getInstance().m15593b(this.f11318a, new aC(this));
            WeightUtil.f11309k = false;
            return null;
        }

        protected void m15721a(Void voidR) {
            if (Keeper.readPersonInfoBaseNeedSync().getNeedSyncServer() != 0) {
                Utils.m7998a(this.f11318a, Keeper.readPersonInfo());
            }
        }

        protected /* synthetic */ Void doInBackground(Void... voidArr) {
            return m15720a(voidArr);
        }

        protected /* synthetic */ void onPostExecute(Void voidArr) {
            m15721a(voidArr);
        }
    }

    public static void m15712b(Context context) {
        if (!f11309k) {
            new aB(context).execute(new Void[f11299a]);
        }
    }

    public static void deleteUserWeights(Context context, int i) { // m15713b
        GuliLog.tracePrintInfo(TAG, "Delete User Weights : " + i);
        List a = WeightInfoManager.getInstance().getInfosByUid(i);
        for (int i2 = f11299a; i2 < a.size(); i2 += f11300b) {
            deleteWeightInfoMarkFully((WeightInfo) a.get(i2));
        }
        LuaUtil.getInstance(context.getApplicationContext()).m15960a(i);
        GuliLog.tracePrintInfo(TAG, "Delete User Weights Finish!!");
    }

//    private static void m15714b(QQHealth qQHealth) {
//        List arrayList;
//        List<WeightInfo> d = WeightInfoManager.getInstance().m15772d();
//        if (d != null && d.size() > 0) {
//            arrayList = new ArrayList(d.size());
//            for (WeightInfo weightInfo : d) {
//                if (weightInfo.uid == f11301c) {
//                    arrayList.add(Long.valueOf(weightInfo.timestamp));
//                }
//            }
//            qQHealth.removeNeedSyncWeights(arrayList);
//        }
//        d = WeightInfoManager.getInstance().m15780f();
//        if (d != null && d.size() > 0) {
//            arrayList = new ArrayList(d.size());
//            for (WeightInfo weightInfo2 : d) {
//                if (weightInfo2.uid == f11301c) {
//                    arrayList.add(Long.valueOf(weightInfo2.timestamp));
//                }
//            }
//            qQHealth.addNeedSyncWeights(arrayList);
//        }
//    }

    public static float m15715c(float f) {
        return m15716c(f, f11300b);
    }

    public static float m15716c(float f, int i) {
        return new BigDecimal((double) f).setScale(i, 4).floatValue();
    }

    public static float m15717c(float f, int i, int i2) {
        switch (i) {
            case f11299a /*0*/:
                switch (i2) {
                    case f11299a /*0*/:
                        return f;
                    case f11300b /*1*/:
                        return m15716c(2.20462f * f, f11302d);
                    case C4220a.bp /*16*/:
                        return f * 2.0f;
                    default:
                        return f;
                }
            case f11300b /*1*/:
                switch (i2) {
                    case f11299a /*0*/:
                        return m15716c(0.45359f * f, f11302d);
                    case f11300b /*1*/:
                        return f;
                    case C4220a.bp /*16*/:
                        return m15716c(0.90718f * f, f11302d);
                    default:
                        return 0.0f;
                }
            case C4220a.bp /*16*/:
                switch (i2) {
                    case f11299a /*0*/:
                        return f / 2.0f;
                    case f11300b /*1*/:
                        return m15716c(1.10231f * f, f11302d);
                    case C4220a.bp /*16*/:
                        return f;
                    default:
                        return 0.0f;
                }
            default:
                return f;
        }
    }

    public static int m15718c(Context context) {
        return !Utils.m8083k() ? Constant.at : Constant.at;
    }

    public static String m15719d(float f, int i, int i2) {
        float f2 = (f11305g * f) * f;
        GuliLog.tracePrintInfo(TAG, "lightWeight " + f2);
        int c = (int) (((double) (m15717c(f2, f11299a, i2) * 10.0f)) + 0.5d);
        GuliLog.tracePrintInfo(TAG, c + C3657a.f18328f);
        f2 = ((float) c) / 10.0f;
        GuliLog.tracePrintInfo(TAG, "after lightWeight " + f2 + "unit " + i2);
        float c2 = ((float) ((int) (((double) (m15717c((f11306h * f) * f, f11299a, i2) * 10.0f)) + 0.5d))) / 10.0f;
        float c3 = ((float) ((int) (((double) (m15717c((f11307i * f) * f, f11299a, i2) * 10.0f)) + 0.5d))) / 10.0f;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(f2 + ",");
        stringBuilder.append(c2 + ",");
        stringBuilder.append(c3 + ",");
        return stringBuilder.toString();
    }
}
